package com.example.areaclean;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class man_area_activity extends AppCompatActivity {
    private ListView listView;
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    RequestQueue queue;
    private String[] area;
    private Intent intent;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_man_area);

        queue= Volley.newRequestQueue(getApplicationContext());
        pref=getSharedPreferences("data",MODE_PRIVATE);

        area=jsonRequest(pref.getString("userNo",""));
    }


    public String[] jsonRequest(String userNo){
        String url="http://192.168.31.32/tp/public/index.php/api/man_area";

        HashMap<String, String> params = new HashMap<>();
        params.put("man_no",userNo);
        JSONObject jsonObject = new JSONObject(params);

        JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        String a="0";
                        try {
                            a=jsonObject.getString("man_area");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        area=a.split("#");
//                        Log.d("WWWWW", area[0]+area[1]+area[2]+"#"+area[3]);
//                        Log.d("AAAAA", area.length+"");

                        listviewInit();

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.d("TAG","fuckError"+volleyError);
                    }
                });
        queue.add(jsObjRequest);
        return area;
    }

    public void listviewInit(){
        listView=findViewById(R.id.area_list);//在视图中找到ListView
        String area1[]={"aa","bb","cc"};
//                        Log.d("WWWWW", area[0]+area[1]+area[2]+"#"+area[3]);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(man_area_activity.this,android.R.layout.simple_list_item_1,area);//新建并配置ArrayAapeter
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(man_area_activity.this,man_select.class);
                intent.putExtra("c_no1",area[i]);
                startActivity(intent);
            }
        });
    }
}



